# Maintainer: Maksim Bondarenkov <maksapple2306@gmail.com>

_realname=wakatime
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=1.115.3
pkgrel=1
pkgdesc="Command line interface used by all WakaTime text editor plugins (mingw-w64)"
arch=('any')
mingw_arch=('ucrt64' 'clang64' 'clangarm64')
url="https://github.com/wakatime/wakatime-cli"
msys2_references=(
  'aur: wakatime'
)
license=('spdx:BSD-3-Clause')
makedepends=("${MINGW_PACKAGE_PREFIX}-go"
             "${MINGW_PACKAGE_PREFIX}-cc"
             'git')
source=("git+${url}.git#tag=v${pkgver}")
sha256sums=('ffd5ccdae46dcee2cf05f81c488b73951d547372e4bed17f2ea3f08aa4fab6a1')

build() {
  # provide date but make it reproducible
  local _date="$(date --utc --date="@${SOURCE_DATE_EPOCH:-$(date +%s)}" '+%Y-%m-%dT%H:%M:%S %Z')"
  local _repo="${url#https://}"

  cd "${_realname}-cli"

  export CGO_CPPFLAGS="${CPPFLAGS}"
  export CGO_CFLAGS="${CFLAGS}"
  export CGO_CXXFLAGS="${CXXFLAGS}"
  export CGO_LDFLAGS="${LDFLAGS}"

  go build -o "build-${MSYSTEM}/${_realname}.exe" \
    -trimpath \
    -buildmode=pie \
    -mod=readonly \
    -modcacherw \
    -ldflags "-linkmode external -extldflags '${LDFLAGS}' \
    -X '${_repo}/pkg/version.OS=$(go env GOOS)' \
    -X '${_repo}/pkg/version.Arch=$(go env GOARCH)' -X '${_repo}/pkg/version.BuildDate=${_date}' \
    -X '${_repo}/pkg/version.Commit=$(git rev-parse --short HEAD)' \
    -X '${_repo}/pkg/version.Version=${pkgver}'"
}

package() {
  cd "${_realname}-cli"

  install -Dm755 "build-${MSYSTEM}/${_realname}.exe" -t "${pkgdir}${MINGW_PREFIX}/bin"
  install -Dm644 LICENSE "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE"
}
